﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;

namespace MBSBusinessEntities
{
    public class FoodBE:ICrud
    {

        public FoodBE()
        {
            this.mbs = new MBS_Entities();
        }

        #region ClassVariables
        
        private MBS_Entities mbs;
        private int _FoodID;
        private string _Category;
        private string _ImageUrl;
        private string _Name;
        private decimal _Price;
        private bool _IsDeleted;
        
        #endregion


        #region Properties
        
        public int FoodID
        {
            get { return _FoodID; }
            set { _FoodID = value; }
        }

        public string Category
        {
            get { return _Category; }
            set { _Category = value; }
        }

        public string ImageUrl
        {
            get { return _ImageUrl; }
            set { _ImageUrl = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public decimal Price
        {
            get { return _Price; }
            set { _Price = value; }
        }


        public bool IsDeleted
        {
            get { return _IsDeleted; }
            set { _IsDeleted = value; }
        }
        
        #endregion


        #region Methods
       
        public  List<FoodBE> getAllFoodItems()
        {
            List<FoodBE> lstFoodEntites = new List<FoodBE>();
            List<Food> lstFoodDALItems = getDALFood();
            foreach (Food foodItem in lstFoodDALItems)
            {
                lstFoodEntites.Add(ConvertToEntity(foodItem));
            }
            return lstFoodEntites;
        }

        private FoodBE ConvertToEntity(Food foodItem)
        {
            FoodBE foodEntity = new FoodBE();
            foodEntity.FoodID = foodItem.FoodID;
            foodEntity.Category = foodItem.Category;
            foodEntity.ImageUrl = foodItem.ImageURL;
            foodEntity.Name = foodItem.Name;
            foodEntity.Price = Convert.ToDecimal(foodItem.Price);
            foodEntity.IsDeleted = foodItem.IsDeleted;
            return foodEntity;
        }

        private List<Food> getDALFood()
        {
            var items=mbs.Foods
                .Where(X=> X.IsDeleted.Equals(false));
                      
            return items.ToList<Food>();
        }

        private Food getDALFoodbyID(int id)
        {
            Food food = new Food();
            food = mbs.Foods.First(X => X.FoodID == id);
            return food;
        }
       
        public bool Add()
        {

            Food foodItem = new Food();
            foodItem.FoodID = this.FoodID;
            foodItem.Category = this.Category;
            foodItem.Name = this.Name;
            foodItem.ImageURL =this.ImageUrl;
            foodItem.Price = this.Price;
            foodItem.IsDeleted = false;
            mbs.AddToFoods(foodItem);
            mbs.SaveChanges();
            return true;
        }

        public bool Delete(int id)
        {
            Food foodItem = mbs.Foods.SingleOrDefault(X => X.FoodID == id);

            foodItem.IsDeleted = true;

            mbs.SaveChanges();
            return true;
        }

        public bool Update()
        {
            Food foodItem = new Food();
            foodItem.FoodID = this.FoodID;
            foodItem.Category = this.Category;
            foodItem.Name = this.Name;
            foodItem.ImageURL = this.ImageUrl;
            foodItem.Price = this.Price;
            foodItem.IsDeleted = false;
            mbs.SaveChanges();
            return true;
        }

        public object GetByID(int id)
        {
            return ConvertToEntity(getDALFoodbyID(id));
        }
        #endregion
    }
}
